package TankFrst;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.util.List;

public class TankServer {
    private static int ID = 100;//坦克的Id号
    public static final int TCP_PORT = 8888;//定义TCP端口号
    public static final int UDP_PORT = 6666;//定义UDP端口号
    List<Client> clients = new ArrayList<Client>();//建立CLient集合，便于储存连接的客户端
//利用方法来建立连接
    public void start() {
        new Thread(new UDPThread()).start();
        ServerSocket serverSocket = null;
        try {
            serverSocket = new ServerSocket(TCP_PORT);
        } catch (IOException e) {
            e.printStackTrace();
        }

        while (true) {
            Socket s = null;
            try {
                s = serverSocket.accept();
                //UDP连接
                DataInputStream dis = new DataInputStream(s.getInputStream());
                String IP = s.getInetAddress().getHostAddress();//IP地址
                int udpPort = dis.readInt();
                Client c = new Client(IP, udpPort);
                clients.add(c);
                //写入Id号码
                DataOutputStream dos = new DataOutputStream(s.getOutputStream());
                dos.writeInt(ID++);//创建其他的id的时候让id号自加
                System.out.println("A Client Connect! Address:" + s.getInetAddress() + ":" + s.getPort());
            } catch (IOException e) {
                e.printStackTrace();
            } finally {//最后关闭
                if (s != null) {
                    try {
                        s.close();
                        s = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public static void main(String[] args) {
        new TankServer().start();

    }

    private class Client {
        String IP;
        int udpPort;

        public Client(String IP, int udpPort) {
            this.IP = IP;
            this.udpPort = udpPort;
        }
    }

    private class UDPThread implements Runnable {
//接受信息，监听，转给其他客户端
        byte[] buf = new byte[1024];
        @Override
        public void run() {
            DatagramSocket ds = null;
            try {
                ds = new DatagramSocket(UDP_PORT);
            } catch (SocketException e) {
                e.printStackTrace();
            }
            System.out.println("UDP thread started at port :" + UDP_PORT);
            //接受数据包
            while (ds != null) {
                DatagramPacket dp = new DatagramPacket(buf, buf.length);
                try {
                    ds.receive(dp);
                    //转发给每个客户端,没收到一个包发送给其他的客户端
                    for (int i = 0; i < clients.size(); i++) {
                        Client c=clients.get(i);
                        dp.setSocketAddress(new InetSocketAddress(c.IP,c.udpPort));
                        ds.send(dp);//发送连接上的消息
                    }
                    System.out.println("a packet received!");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
